package hu.ikti.oamadmin.types.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.jboss.seam.annotations.Name;

@Entity
@Name("bundle")
@Table(name = "BUNDLE")
@SequenceGenerator(sequenceName = "BUNDLE_SEQ", name = "B_SEQ", allocationSize = 1)
public class Bundle implements Serializable {

	private static final long serialVersionUID = 1L;

	private Long id;
	private BundleData bundleData;
	private Bundle parent;
	private Service service;
	private ServiceProfile serviceProfile;
	private Gateway gateway;
	private List<Resource> resources = new ArrayList<Resource>();

	private boolean selected = false;

	@Id
	@Column(name = "ID")
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "B_SEQ")
	public Long getId() {
		return id;
	}

	@OneToOne
	@JoinColumn(name = "BUNDLEDATA_FK", nullable = false)
	public BundleData getBundleData() {
		return bundleData;
	}

	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PARENT_BUNDLE")
	public Bundle getParent() {
		return parent;
	}

	@ManyToOne
	@JoinColumn(name = "SERVICE_FK")
	public Service getService() {
		return service;
	}

	@ManyToOne
	@JoinColumn(name = "SERVICEPROFILE_FK")
	public ServiceProfile getServiceProfile() {
		return serviceProfile;
	}

	@ManyToOne
	@JoinColumn(name = "GATEWAY_FK")
	public Gateway getGateway() {
		return gateway;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinTable(name = "BUNDLE_RESOURCE_RT", joinColumns = { @JoinColumn(name = "BUNDLE_FK", referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "RESOURCE_FK", referencedColumnName = "ID") })
	public List<Resource> getResources() {
		return resources;
	}

	@Transient
	public boolean isSelected() {
		return selected;
	}

	public void setBundleData(BundleData bundleData) {
		this.bundleData = bundleData;
	}

	public void setParent(Bundle parent) {
		this.parent = parent;
	}

	public void setService(Service service) {
		this.service = service;
	}

	public void setServiceProfile(ServiceProfile serviceProfile) {
		this.serviceProfile = serviceProfile;
	}

	public void setGateway(Gateway gateway) {
		this.gateway = gateway;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public void setResources(List<Resource> resources) {
		this.resources = resources;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((bundleData == null) ? 0 : bundleData.hashCode());
		result = prime * result + ((gateway == null) ? 0 : gateway.hashCode());
		result = prime * result + ((service == null) ? 0 : service.hashCode());
		result = prime * result
				+ ((serviceProfile == null) ? 0 : serviceProfile.hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Bundle other = (Bundle) obj;
		if (bundleData == null) {
			if (other.bundleData != null)
				return false;
		} else if (!bundleData.equals(other.bundleData))
			return false;
		if (gateway == null) {
			if (other.gateway != null)
				return false;
		} else if (!gateway.equals(other.gateway))
			return false;
		if (service == null) {
			if (other.service != null)
				return false;
		} else if (!service.equals(other.service))
			return false;
		if (serviceProfile == null) {
			if (other.serviceProfile != null)
				return false;
		} else if (!serviceProfile.equals(other.serviceProfile))
			return false;
		return true;
	}

}
